package com.csxy.six.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.six.entity.Announcement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 公告Mapper接口
 * 提供公告相关的数据库操作
 */
@Mapper
public interface AnnouncementMapper extends BaseMapper<Announcement> {

    /**
     * 分页查询公告（带条件）
     * @param page 分页对象
     * @param title 标题关键词（模糊查询）
     * @param categoryId 分类ID
     * @param isTop 是否置顶
     * @param isPublished 是否已发布
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT * FROM announcement " +
            "<where>" +
            "   <if test='title != null and title != \"\"'>" +
            "       AND title LIKE CONCAT('%', #{title}, '%')" +
            "   </if>" +
            "   <if test='categoryId != null'>" +
            "       AND category_id = #{categoryId}" +
            "   </if>" +
            "   <if test='isTop != null'>" +
            "       AND is_top = #{isTop}" +
            "   </if>" +
            "   <if test='isPublished != null'>" +
            "       AND is_published = #{isPublished}" +
            "   </if>" +
            "</where>" +
            "ORDER BY is_top DESC, publish_time DESC" +
            "</script>")
    IPage<Announcement> selectPageWithCondition(Page<Announcement> page,
                                                @Param("title") String title,
                                                @Param("categoryId") Integer categoryId,
                                                @Param("isTop") Boolean isTop,
                                                @Param("isPublished") Boolean isPublished);

    /**
     * 更新置顶状态
     * @param announceId 公告ID
     * @param isTop 是否置顶
     * @return 影响行数
     */
    @Update("UPDATE announcement SET is_top = #{isTop} WHERE announce_id = #{announceId}")
    int updateTopStatus(@Param("announceId") Integer announceId,
                        @Param("isTop") Boolean isTop);

    /**
     * 更新发布状态
     * @param announceId 公告ID
     * @param isPublished 是否发布
     * @return 影响行数
     */
    @Update("UPDATE announcement SET is_published = #{isPublished} WHERE announce_id = #{announceId}")
    int updatePublishStatus(@Param("announceId") Integer announceId,
                            @Param("isPublished") Boolean isPublished);
}